<template>
	<view class="container">
		<view class="queryForm">
			<uni-forms ref="queryRef" :model="queryParams" label-width="70px" label-align="right">
				<uni-forms-item label="任务名称">
					<uni-easyinput v-model="queryParams.jobName" placeholder="请输入任务名称" />
				</uni-forms-item>
				<uni-forms-item label="任务组名">
					<xy-select v-model="queryParams.jobGroup" :data="sys_job_group" placeholder="任务组名" clearable />
				</uni-forms-item>
				<uni-forms-item label="任务状态" name="status">
					<xy-select v-model="queryParams.status" :data="sys_job_status" placeholder="任务状态" clearable />
				</uni-forms-item>
				<uni-forms-item label="操作">
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="handleQuery()">搜索</button>
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="resetQuery()">重置</button>
					<button class="mini-btn xy-btn" type="default" size="mini" @click="handleAdd()"
						v-if="proxy.$auth.hasPermi('monitor:job:add')">添加</button>
					<button class="mini-btn xy-btn" type="default" size="mini" @click="handleJobLog()"
						v-if="proxy.$auth.hasPermi('monitor:job:query')">调度日志</button>
				</uni-forms-item>
			</uni-forms>
		</view>
		<!-- @iconClick="iconClick" -->
		<view v-for="(item,index) in jobList" :key="index">
			<uni-card>
				<view class="uni-body">
					<view>
						<text>任务编号：</text>
						<text>{{ item.jobId }}</text>
					</view>
					<view>
						<text>任务名称：</text>
						<text>{{ item.jobName }}</text>
					</view>
					<view>
						<text>任务组名：</text>
						<dict-tag :options="sys_job_group" :value="item.jobGroup" />
					</view>
					<view>
						<text>调用目标字符串：</text>
						<text>{{ item.invokeTarget }}</text>
					</view>
					<view>
						<text>cron执行表达式：</text>
						<text>{{ item.cronExpression }}</text>
					</view>
					<view>
						<text>状态：</text>
						<dict-tag :options="sys_job_status" :value="item.status" />
						<xy-switch v-model="item.status" active-value="0" inactive-value="1"
							@change="handleStatusChange(item)" />
					</view>
					<view>
						<text>创建时间：</text>
						<text>{{ item.createTime }}</text>
					</view>
				</view>
				<view slot="actions" class="card-actions">
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="handleUpdate(item)"
						v-if="proxy.$auth.hasPermi('monitor:job:edit')">编辑</button>
					<button class="mini-btn xy-btn" type="warn" size="mini" @click="handleDelete(item)"
						v-if="proxy.$auth.hasPermi('monitor:job:remove')">删除</button>
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="handleRun(item)"
						v-if="proxy.$auth.hasPermi('monitor:job:changeStatus')">立即执行一次</button>
					<button class="mini-btn xy-btn" type="default" size="mini" @click="handleJobLog(item)"
						v-if="proxy.$auth.hasPermi('monitor:job:query')">调度日志</button>
				</view>
			</uni-card>
		</view>
		<uni-load-more :status="xlstatus" />
	</view>
</template>

<script setup>
	import {
		listJob,
		getJob,
		delJob,
		addJob,
		updateJob,
		runJob,
		changeJobStatus
	} from "@/pages_work/api/monitor/job";
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		onLoad,
		onPullDownRefresh,
		onReachBottom,
	} from '@dcloudio/uni-app'
	const {
		proxy
	} = getCurrentInstance();
	const {
		sys_job_group,
		sys_job_status
	} = proxy.useDict("sys_job_group", "sys_job_status");

	/** 查询任务列表所需的参数 */
	const queryParams = ref({
		pageNum: 1,
		pageSize: 10,
	});
	/** 总数 */
	const total = ref(0);
	/** 任务列表 */
	const jobList = ref([]);
	/** 下拉状态 */
	const xlstatus = ref('more');

	/** 查询任务列表 */
	const getList = () => {
		listJob(queryParams.value).then(res => {
			jobList.value = jobList.value.concat(res.rows);
			total.value = res.total;

			if (res.total == undefined) {
				// 标记加载数据完成
				xlstatus.value = 'more';
			} else if (res.total <= jobList.value.length) {
				// 标记加载数据完成
				xlstatus.value = 'noMore';
			} else {
				// 标记还有剩余数据
				xlstatus.value = 'more';
			}
		})
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		xlstatus.value = 'more';
		queryParams.value.pageNum = 1;
		jobList.value = [];
		getList();
	};

	/** 重置按钮操作 */
	function resetQuery() {
		// dateRange.value = [];
		proxy.resetForm("queryRef");
		queryParams.value = {
			pageNum: 1,
			pageSize: 10,
		};
		handleQuery();
	};

	/** 添加任务信息 */
	const handleAdd = () => {
		uni.$once('update', function(){
			handleQuery()
		})
		proxy.$tab.navigateTo("form?title=添加任务&jobId=");
	}

	/** 编辑任务信息 */
	const handleUpdate = (row) => {
		uni.$once('update', function(){
			handleQuery()
		})
		proxy.$tab.navigateTo("form?title=编辑任务&jobId=" + row.jobId);
	}

	/** 任务日志列表查询 */
	const handleJobLog = (row) => {
		if (row == undefined) {
			proxy.$tab.navigateTo("log?title=任务日志列表");
			return
		}
		proxy.$tab.navigateTo("log?title=任务日志列表&jobId=" + row.jobId);
	}

	/** 删除按钮操作 */
	const handleDelete = (row) => {
		const ids = row.jobId;
		proxy.$modal.confirm('是否确认删除任务编号为"' + ids + '"的数据项？').then(function() {
			return delJob(ids);
		}).then(() => {
			handleQuery();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	};

	/** 任务状态修改  */
	const handleStatusChange = (row) => {
		let text = row.status === "0" ? "启用" : "停用";
		proxy.$modal.confirm('确认要"' + text + '""' + row.jobName + '"任务吗?').then(function() {
			return changeJobStatus(row.jobId, row.status);
		}).then(() => {
			proxy.$modal.msgSuccess(text + "成功");
		}).catch(function() {
			row.status = row.status === "0" ? "1" : "0";
		});
	};

	/* 立即执行一次 */
	function handleRun(row) {
		proxy.$modal.confirm('确认要立即执行一次"' + row.jobName + '"任务吗?').then(function() {
				return runJob(row.jobId, row.jobGroup);
			}).then(() => {
				proxy.$modal.msgSuccess("执行成功");
			})
			.catch(() => {});
	}

	/** 生命周期函数--下拉刷新 */
	onPullDownRefresh(() => {
		handleQuery()
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 1000)
	})

	/** 生命周期函数-上拉加载 */
	onReachBottom(() => {
		if (xlstatus.value == 'noMore' || xlstatus.value == 'loading') {
			// 如果没有更多数据或者正在加载数据，则不需要加载更多，直接返回
			return;
		}
		// 标记正在加载数据
		xlstatus.value = 'loading';
		queryParams.value.pageNum++;
		getList();
	})

	handleQuery();
</script>

<style lang="scss">
	@import "@/static/scss/work/index.scss";
</style>